﻿using System;
using System.Web.Mvc;
using System.Text.RegularExpressions;
using NHibernate;
using Mailer.Model;
using Mailer.Model.Types;

namespace Mailer.Controllers
{
	[HandleError]
	public class RecipientController : Controller
	{
		[Authorize]
		public ActionResult View(Guid id)
		{
			// TODO: kolla access
			
			var sess = SessionUtilities.BeginSession();
			if (sess != null)
			{
				var recip = sess.Get<Recipient>(id);
				if (recip != null)
				{
					var app = sess.Get<Application>(recip.ApplicationID);

					ViewData["ID"] = recip.ID;

					ViewData["ApplicationID"] = recip.ApplicationID;

					ViewData["Email"] = recip.Email;
					ViewData["DisplayName"] = recip.GetPropertyValue("displayname");

					ViewData["AllLists"] = app.Lists;
					ViewData["Lists"] = recip.Lists;
				}

				SessionUtilities.EndSession(sess);
			}

			return base.View();
		}




		[Authorize]
		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult New()
		{
			return base.View();
		}

		[Authorize]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult New(string email)
		{
			ActionResult res = null;
			
			var sess = SessionUtilities.BeginSession();
			if (sess != null)
			{
				var userid = Utilities.GetUserID(this);

				if (Regex.IsMatch("@", email))
				{
					ModelState.AddModelError("_FORM", "You must enter a valid e-mail adress.");
				}
				else
				{
					// TODO: Check for duplicate shortnames!

					var appid = new Guid(Request["application"]);

					using (ITransaction transaction = sess.BeginTransaction())
					{
						var recip = new Recipient();
						recip.Email = email;
						recip.CreatedBy = userid;
						recip.UpdatedBy = userid;
						recip.CreatedDate = DateTime.Now;
						recip.UpdatedDate = DateTime.Now;

						sess.Save(recip);

						var app = sess.Get<Application>(appid);
						app.Recipients.Add(recip);
						sess.Save(app);

						res = RedirectToAction("View", "Application", new { ID = appid });

						transaction.Commit();
					}
				}

				SessionUtilities.EndSession(sess);
			}
			// If we got this far, something failed, redisplay form
			if (res == null)
				res = base.View();

			return res;
		}





		[Authorize]
		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Join(Guid list, Guid recipient)
		{ 
			var sess = SessionUtilities.BeginSession();
			if (sess != null)
			{
				// var userid = Utilities.GetUserID(this);

				using (var transaction = sess.BeginTransaction())
				{
					var recipientobj = sess.Get<Recipient>(recipient);

					var listobj = sess.Get<List>(list);
					if (!recipientobj.Lists.Contains(listobj))
						recipientobj.Lists.Add(listobj);

					sess.Save(recipientobj);

					transaction.Commit();
				}

				SessionUtilities.EndSession(sess);
			}

			return RedirectToAction("View", "Recipient", new { ID = recipient });
		}



		[Authorize]
		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Leave(Guid list, Guid recipient)
		{ 
			var sess = SessionUtilities.BeginSession();
			if (sess != null)
			{
				// var userid = Utilities.GetUserID(this);

				using (var transaction = sess.BeginTransaction())
				{
					var recipientobj = sess.Get<Recipient>(recipient);

					var listobj = sess.Get<List>(list);
					if (recipientobj.Lists.Contains(listobj))
						recipientobj.Lists.Remove(listobj);

					sess.Save(recipientobj);

					transaction.Commit();

				}

				SessionUtilities.EndSession(sess);
			}

			return RedirectToAction("View", "Recipient", new { ID = recipient });
		}



	}
}
